package com.cheyun.netsalev3.viewmodel

import android.util.Log
import android.view.View
import com.cheyun.netsalev3.bean.dm.DmLogin
import com.cheyun.netsalev3.bean.login.LoginParam
import com.cheyun.netsalev3.utils.CommonFunc
import com.cheyun.netsalev3.utils.MyIntent
import com.cheyun.netsalev3.utils.MySharedPreferences
import com.cheyun.netsalev3.utils.api.*
import com.cheyun.netsalev3.utils.api.dm.DmApiCallback
import com.cheyun.netsalev3.utils.api.dm.DmApiException
import com.cheyun.netsalev3.view.MainActivity
import com.cheyun.netsalev3.view.dm.DmMainActivity
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers


/**
 * @author Michael
 * @date  2018/12/6 - 10:48.
 */
class ModuleActivityViewModel : BaseViewModel() {
    var password: String = MySharedPreferences.getPassword()
    var mobile: String = MySharedPreferences.getMobile()
    var version:String = "";
    var skey:String = "";

    fun onClickCrm(view: View) {
        if(skey.equals("api.v4.login.mobile.channel")){
            MyToast("未绑定经销商")
            return;
        }
        var pp = CommonFunc.textToMD5L32(password)
        ApiClient.getInstance().Login(mobile, pp)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<LoginParam>>() {
                    override fun onSuccess(t: BaseResp<LoginParam>) {
                        MySharedPreferences.putLastModule("crm")
                        var intent = MyIntent(MainActivity::class.java)
                        myStartActivity(intent)
                        myFinish()
                    }

                    override fun onError(ex: ApiException) {
                        if(ex.getSkey().equals("api.v4.login.mobile.channel")){
                            MyToast("未绑定经销商")
                        }
                    }
                })
    }



    fun onClickDm(view: View) {
        goDm()
    }

    fun goDm(){
        var channel = MySharedPreferences.getChannel();
        DmApiClient.getInstance().login(mobile,CommonFunc.textToMD5L32(password), channel)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : DmApiCallback<DmBaseResp<DmLogin, Any>>() {
                    override fun onSuccess(t: DmBaseResp<DmLogin, Any>) {
                        t.list.apply {
                            MySharedPreferences.putDMCauthkey(this?.auth.toString())
                            MySharedPreferences.putDMMgr(this?.clue_mgr!!)
                            MySharedPreferences.putDmLoginData(this);
                            var intent = MyIntent(DmMainActivity::class.java)
                            intent.putExtra("showMy",true);
                            myStartActivity(intent)
                            MySharedPreferences.putLastModule("dm")
                            myFinish()
                        }
                    }
                    override fun onError(ex: DmApiException) {
                        Log.e("dm login",ex.getMessage())
                        MyToast(ex.getMessage()!!);
                    }
                })
    }


}